<template>
  <div class="swiper-container" ref="cur">
    <div class="swiper-wrapper" @click="gosearch">
      <div class="swiper-slide" v-for="carousel in list" :key="carousel.id">
        <img :src="carousel.imgUrl" :data-title="carousel.title" />
      </div>
    </div>

    <div class="swiper-pagination"></div>
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
  </div>
</template>
<script>
import Swiper from "swiper";
export default {
  name: "MyCarousel",
  props: ["list"],
  watch: {
    list: {
      immediate: true,

      handler() {
        this.$nextTick(() => {
          new Swiper(this.$refs.cur, {
            loop: true, // 循环

            pagination: {
              el: ".swiper-pagination",
              clickable: true,
            },

            navigation: {
              nextEl: ".swiper-button-next",
              prevEl: ".swiper-button-prev",
            },
          });
        });
      },
    },
  },
  methods: {
    gosearch(event) {
      let element = event.target;
      let { title } = element.dataset;
      if (title) {
        let location = {
          name: "Search",
        };
        let query = {
          keyword: title,
        };
        if (this.$route.params) {
          location.params = this.$route.params;
          location.query = query;
          this.$router.push(location);
        }
      }
    },
  },
};
</script>
